//
//  NvDraftManager.m
//  NvSellerShow
//
//  Created by Meicam on 2017/3/14.
//  Copyright © 2017年 Meicam. All rights reserved.
//

#import "NvDraftManager.h"
#import "Utils.h"

@implementation NvDraftManager
+ (NSArray *)getAllDrafts {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults arrayForKey:DraftArrayKey];
}
+ (void)addDraft:(NSDictionary *)draftDic {
    NSMutableArray *draftArray = [self getAllDrafts].mutableCopy;
    if(draftArray == nil)
        draftArray = [NSMutableArray array];
    [draftArray insertObject:draftDic atIndex:0];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:draftArray forKey:DraftArrayKey];
    [userDefaults synchronize];
    
}
+ (void)deleteDraftAtIds:(NSArray *)draftIds {
    NSMutableArray *draftArray = [self getAllDrafts].mutableCopy;
    if(draftArray == nil)
        draftArray = [NSMutableArray array];
    for(NSString *draftId in draftIds) {
        [draftArray enumerateObjectsUsingBlock:^(NSDictionary *draftDic, NSUInteger idx, BOOL *stop) {
            NSString *drafName = [draftDic valueForKey:DraftNameKey];
            if([drafName isEqualToString:draftId]){
                [draftArray removeObject:draftDic];
                *stop = YES;
            }
        }];

    }
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:draftArray.copy forKey:DraftArrayKey];
    [userDefaults synchronize];
}

+ (void)deleteAllDraft {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:nil forKey:DraftArrayKey];
    [userDefaults synchronize];
}
@end
